package com.zj.exception;

import com.zj.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public R handler(Exception e){
        log.error(e.getMessage());
        if (e instanceof ServiceException){
            ServiceException serviceException = (ServiceException) e;
            return R.data(serviceException.getCode(),serviceException.getMessage());
        }else {
            return R.error(e.getMessage());
        }
    }
}
